/*****************************************************
** Author:
** Date: 2019-10-19
*
*****************************************************/

#include <stdarg.h>
#include "comm.h"

/**
 * @brief
 * @param  None
 * @retval None
 */
void dump_memory(const unsigned char *buff, unsigned int len, char *str, ...)
{
    if(buff == (void *)0 || len == 0)
        return ;

    va_list arg;
    int idx;

    printf("[DUMP] size %d ", len);

    va_start(arg, str);
    vprintf(str, arg);
    va_end(arg);

    for(idx = 0; idx < len; idx++)
    {
        if ((idx % 16) == 0)
            printf("\r\n%p:", &buff[idx]);

        printf(" %02x", buff[idx]);
    }
    printf("\r\n");
}

int str2hex(char *str, unsigned char *strhex, int maxsize)
{
    char *p = str;
    int val, cnt = 0, slen = strlen(str);

    if ((slen & 0x01) || ((slen >> 1) > maxsize)) {
        log_err("strlen %d err maxsize %d", slen, maxsize);
        return -1;
    }

    while(*p != '\0') {

        if ((*p >= '0') && (*p <= '9'))
            val = *p - '0';
        else if ((*p >= 'A') && (*p <= 'Z'))
            val = *p - 'A' + 0x0a;
        else if ((*p >= 'a') && (*p <= 'z'))
            val = *p - 'a' + 0x0a;
        else
            val = 0;

        if (cnt & 0x01) // this is low 4 bits
            strhex[cnt>>1] |= val;
        else    // this is high 4 bits
            strhex[cnt>>1] = val << 4;

        p++;
        cnt++;
    }

    return (cnt >> 1);
}

/* End of file */



